package mysgoog.pattern.dragon;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;

import mysgoog.board.Board;
import mysgoog.bot.*;
import mysgoog.gtp.*;
import mysgoog.pattern.Matcher;
import mysgoog.pattern.Storage.VertexList;
import mysgoog.pattern.Storage.VertexValue;
import mysgoog.util.BoardUtil;
/**
* @author Fu, Zhongyang
*/
public class ConnPatternMatcher
{

    Map<Integer, Map<String, VertexList>> patternMap;
    int count;
  
    
    public ConnPatternMatcher()
    {
        patternMap = new HashMap<Integer, Map<String, VertexList>>();
        try {
			ConnPatternLoader.load(patternMap);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
    }
    
	public List<VertexList> getMatch(Board b, Color player) {
		List<VertexList> moves = new ArrayList<VertexList>();
		return getMatch(b, player, moves);
	}

	private List<VertexList> getMatch(Board b, Color player, List<VertexList> moves) {
		Color[] board = b.getBoard();
		for (int key:patternMap.keySet()){
			int row = key / Config.getBoardSize();
			int column = key % Config.getBoardSize();
			getMatch(row, column, patternMap.get(key), board, player, moves);
		}
		return moves;
	}
	
	//for test
	private List<VertexList> getMatch(Color[] board, Color player, List<VertexList> moves) {		
		for (int key:patternMap.keySet()){
			int row = key / Config.getBoardSize();
			int column = key % Config.getBoardSize();
			getMatch(row, column, patternMap.get(key), board, player, moves);
		}
		return moves;
	}
	
	
	
	private void getMatch(int row, int column, Map<String, VertexList> map, Color[] board, Color player, List<VertexList> moves){
		//System.out.println(count++);
		Color[] board90 = BoardUtil.rotateBoard(Config.getBoardSize(), Config.getBoardSize(), board, 90);
		Color[] board270 = BoardUtil.rotateBoard(Config.getBoardSize(), Config.getBoardSize(), board, 270);
		Color[] board180 = BoardUtil.rotateBoard(Config.getBoardSize(), Config.getBoardSize(), board, 180);
		
		for (int i = -1; i < Config.getBoardSize() - row; i++){
			for (int j = -1; j < Config.getBoardSize() - column; j++){
				String subBoard;				
				subBoard = BoardUtil.getSubBoardString(i, j, row, column, board, player);	
				if (map.containsKey(subBoard)){		
					moves.add(
							parseValueList(map.get(subBoard), i, j, 0));
				}
				subBoard = BoardUtil.getSubBoardString(i, j, row, column, board90, player);
				if (map.containsKey(subBoard)){					
					moves.add(
							parseValueList(map.get(subBoard), i, j, 90));
				}				
				subBoard = BoardUtil.getSubBoardString(i, j, row, column, board180, player);
				if (map.containsKey(subBoard)){					
					moves.add(
							parseValueList(map.get(subBoard), i, j, 180));
				}
				subBoard = BoardUtil.getSubBoardString(i, j, row, column, board270, player);
				if (map.containsKey(subBoard)){					
					moves.add(
							parseValueList(map.get(subBoard), i, j, 270));
				}							
							
			}
		}
	}
	
	private VertexList parseValueList(VertexList vp, int x, int y, int degree){
		VertexList vv = new VertexList(vp.type); 
		for (Vertex v:vp.list){
			vv.list.add(BoardUtil.rotateVertex(
						BoardUtil.changeVertex(v, x, y), 
						Config.getBoardSize(), 
						degree));
		}
		return vv;
	}
		
}
